package com.itany.controllers;

import com.itany.service.TypeService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    TypeService service;

    //查找所有用户的信息
    @RequestMapping("/findAll")
    public Map<String, Object> findAll(
            @RequestParam("parentid") String parentid,
            @RequestParam("type") Integer type) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = service.queryByParams(parentid, type);
        int total = list != null ? list.size() : 0;
        if(list != null){
            map.put("rows", list);
        }else{
            map.put("rows", 0);
        }
        map.put("total", total);
        return map;
    }

    @RequestMapping("/add")
    public ActionResult add(
            @RequestParam("name") String name,
            @RequestParam("parentid") String parentid,
            @RequestParam("type") Integer type){
        service.insertType(name,parentid,type);
        return ActionResult.success("添加成功!",200,null);
    }

    @RequestMapping("/modify")
    public ActionResult modify(
            @RequestParam("name") String name,
            @RequestParam("id") Integer id){
        service.modifyType(name,id);
        return ActionResult.success("修改成功!",200,null);
    }

    @RequestMapping("/delete")
    public ActionResult delete(
            @RequestParam("id") Integer id,
            @RequestParam("type") Integer type){
        Boolean bool = service.deleteType(id,type);
        if(bool){
            return ActionResult.success("删除成功!",200,null);
        }else{
            return ActionResult.success("该类型被服务类型关联，无法删除!",200,null);
        }
    }
}
